odoo.define('web_ps_report.PsReportView', function (require) {
    'use strict';

    var AbstractView = require('web.AbstractView');
    var BasicView = require('web.BasicView');
    var core = require('web.core');
    var view_registry = require('web.view_registry');
    var PsReportController = require('web_ps_report.PsReportController');
    var PsReportModel = require('web_ps_report.PsReportModel');
    var PsReportRenderer = require('web_ps_report.PsReportRenderer');

    var _t = core._t;
    var _lt = core._lt;

    var intervals = {
        day: _lt('Day'),
        week: _lt('Week'),
        month: _lt('Month'),
        year: _lt('Year'),
    };

    var PsReportView = BasicView.extend({
        display_name: _lt('PsReport'),
        icon: 'fa-signal',
        config: {
            Model: PsReportModel,
            Controller: PsReportController,
            Renderer: PsReportRenderer,
        },
        viewType: 'ps_report',
        groupable: false,
        enableTimeRangeMenu: 'true',
        /**
         * @override
         */
        init: function (viewInfo, params) {
            this._super.apply(this, arguments);

            var fields = this.fields;
            var attrs = this.arch.attrs;

            var ps_fields = this.arch.children;

            var render_type = this.arch.attrs.render_type;
            var modelName = this.loadParams.modelName;


            // Model Parameters
            this.loadParams.model = this.model;
            this.loadParams.fields = this.fields;
            this.loadParams.mode = params.context.ps_report_mode || attrs.mode || 'retention';
            this.loadParams.timeline = params.context.ps_report_timeline || attrs.timeline || 'forward';
            this.loadParams.measure = params.context.ps_report_measure || attrs.measure || '__count__';
            this.loadParams.interval = params.context.ps_report_interval || attrs.interval || 'day';
            this.loadParams.ps_fields = params.context.ps_fields || ps_fields;
            this.loadParams.render_type = params.context.render_type || render_type;
            this.loadParams.modelName = params.modelName || modelName;

            // Renderer Parameters
            var measures = {};
            _.each(fields, function (field, name) {
                if (name !== 'id' && field.store === true && _.contains(['integer', 'float', 'monetary'], field.type)) {
                    measures[name] = field.string;
                }
            });
            measures.__count__ = _t('Count');
            this.rendererParams.measures = measures;
            this.rendererParams.intervals = intervals;
            this.rendererParams.mode = this.loadParams.mode;
            this.rendererParams.timeline = this.loadParams.timeline;
            this.rendererParams.ps_fields = this.loadParams.ps_fields;
            this.rendererParams.modelName = this.loadParams.modelName;
            this.rendererParams.model = this.loadParams.model;
            this.rendererParams.fields = this.loadParams.fields;

            // Controller Parameters
            this.controllerParams.measures = _.omit(measures, '__count__');
            this.controllerParams.intervals = intervals;
            this.controllerParams.title = params.title || attrs.string || _t('Untitled');
            // Used in export
            this.controllerParams.timeline = this.rendererParams.timeline;
            // Retrieve form and list view ids from the action to open those views
            // when a row of the ps_report view is clicked
            this.controllerParams.views = [
                _findViewID('list'),
                _findViewID('form'),
            ];

            function _findViewID(viewType) {
                var action = params.action;

                if (action === undefined) {
                    return [false, viewType];
                }
                var contextID = viewType === 'list' ? action.context.list_view_id : action.context.form_view_id;
                var result = _.findWhere(action.views, {type: viewType});
                return [contextID || (result ? result.viewID : false), viewType];
            }
        },
    });

    view_registry.add('psreport', PsReportView);

    return PsReportView;

});
